<?php $gift =Mage::registry("gift_data"); ?>
<form id="submit_form" method="post" action="<?php echo $this->getUrl('admin_promotion/adminhtml_gift/save'); ?>">
    <input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
    <?php if($gift && $gift->getId()): ?>
          <input name="id" type="hidden" value="<?php echo $gift->getId(); ?>" />
    <?php endif; ?>
    <div class="content-header">
        <table cellspacing="0">
            <tbody><tr>
                <td style="width:50%;"><h3 class="icon-head head-products">买赠管理</h3></td>
                <td class="a-right">
                        <span id="coupons_generate_button"><button id="id_3cf81872242ced1ce5317b5704e032c7" title="保存" type="button" class="scalable generate" onclick="save()"><span><span><span>保存</span></span></span></button></span>
                </td>
            </tr>
        </tbody></table>
    </div>
    <div class="entry-edit">
        <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend">基本信息</h4>
        <div class="form-buttons"></div>
    </div>
    <div class="fieldset  ignore-validate" id="coupons_information_fieldset">
        <div class="hor-scroll">
        <table cellspacing="0" class="form-list">
            <tbody>
                <?php 
                    $websites = Mage::getModel('core/website')->getCollection()->toOptionHash();
                    $websiteId =Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
                 ?>
                <tr>
                    <td class="label"><label for="status">站点<span class="required">*</span></label></td>
                    <td class="value">
                        <select id="website" name="website_id" class="required-entry select">
                        <?php if($gift && $gift->getId()): ?>
                        <option value="<?php echo $gift->getWebsiteId(); ?>"><?php echo $websites[$gift->getWebsiteId()]; ?></option>
                        <?php elseif($websiteId): ?>
                        <option value="<?php echo $websiteId; ?>"><?php echo $websites[$websiteId]; ?></option>  
                        <?php else: ?>
                        <option value="0"></option>
                        <?php foreach ($websites as $key => $website):?>
                        <option value="<?php echo $key ?>"><?php echo $website; ?></option>
                        <?php endforeach; ?>
                        </select>
                        <?php endif; ?>
                    </td>
                    <td class="label"><label for="status">状态<span class="required">*</span></label></td>
                    <td class="value">
                        <?php if($gift && $gift->getId()): ?>
                            <select id="status" name="status" class="required-entry select">
                            <?php if($gift->getStatus()): ?>
                                <option value="1"  selected="selected">启用</option>
                                <option value="0">禁用</option>
                            <?php else: ?>
                                <option value="1">启用</option>
                                <option value="0"  selected="selected">禁用</option>
                            </select>
                            <?php endif;?>
                        <?php else: ?>
                            <select id="status" name="status" class="required-entry select">
                            <option value="1">启用</option>
                            <option value="0">禁用</option>
                            </select>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td class="label"><label for="name">商品<span class="required">*</span></label></td>
                    <?php if($gift && $gift->getId()): $productId =$gift->getProductId(); ?>
                        <td class="value" id="product_name_show">
                             <div class="uimage"><?php echo $this->getImageId($productId ); ?></div>
                             <div>
                             <p>商品SKU:<span class="usku"><?php echo $this->getProductsku($productId); ?></span></p>
                             <p>商品名称：<span class="uproductname"><?php echo $gift->getProductName(); ?></span></p>
                             </div> 
                             <input type="hidden" name="product_id" id="productid" value="<?php echo $gift->getProductId(); ?>" />
                             <input id="product_name" name="product_name"  title="商品名称" class="required-entry input-text"  type="hidden" value="<?php echo $gift->getProductName(); ?>">
                                
                        </td>
                        <td><button onclick="add(1); return false;">修改商品</button></td>
                    <?php else: ?>
                        <td class="value" id="product_name_show" style="display:none;">
                            <div class="uimage"></div>
                            <div>
                            <p>商品SKU:<span class="usku"></span></p>
                            <p>商品名称：<span class="uproductname"></span></p>
                            </div>
                            <input type="hidden" name="product_id" id="productid"/>
                            <input id="product_name" name="product_name"   title="商品名称" class="required-entry input-text"  type="hidden">
                        </td>
                        <td><button onclick="add(1);return false;" id="addproducts" >添加商品</button></td>
                    <?php endif; ?>
                </tr>
                <tr>
                    <td class="label"><label for="name">赠品<span class="required">*</span></label></td>
                    <?php if($gift && $gift->getId()):$giftproductid = $gift->getGiftProductId(); ?>
                        <td class="value" id="gift_product_show">
                            <div class="gimage"><?php echo $this->getImageId($giftproductid ); ?></div>
                            <div>
                            <p>商品SKU:<span class="gsku"><?php echo $this->getProductsku($giftproductid); ?></span></p>
                            <p>商品名称：<span class="gproductname"><?php echo $gift->getGiftProductName();  ?></span></p>
                            </div> 
                            <input type="hidden" name="gift_product_id" id="giftproductid" value="<?php echo $gift->getGiftProductId(); ?>" />
                            <input type="hidden"  id="oldqty" value="<?php echo $this->getOldQty($gift->getGiftProductId(),$gift->getWebsiteId());  ?>" />
                            <input type="hidden"  id="stock" value="<?php echo $this->hasStock($gift->getGiftProductId(),$gift->getWebsiteId());  ?>" />
                            <input id="gift_product_name"  name="gift_product_name"  title="赠品名称" class="required-entry input-text" value="<?php echo $gift->getGiftProductName(); ?>"  type="hidden">     
                        </td>
                        <td><button onclick="add(2);return false;" >修改赠品</button></td>
                    <?php else: ?>
                        <td class="value"  id="gift_product_show" style="display:none;">
                            <div class="gimage"></div>
                            <div>
                            <p>商品SKU:<span class="gsku"></span></p>
                            <p>商品名称：<span class="gproductname"></span></p>
                            </div>
                            <input type="hidden" name="gift_product_id" id="giftproductid"/>
                            <input type="hidden"  id="oldqty"/>
                            <input type="hidden"  id="stock"/>
                            <input id="gift_product_name"  name="gift_product_name"  title="赠品名称" class="required-entry input-text" type="hidden">    
                        </td>
                        <td><button onclick="add(2);return false;" id="addgiftproducts">添加赠品</button></td>
                    <?php endif; ?>
                    
                </tr>
                <tr>
                    <td class="label"><label for="name">买<span class="required">*</span></label></td>
                    <td class="value">
                        <?php if($gift && $gift->getId()): ?>
                            <input id="discount_step" name="discount_step"  title="折扣步骤" class="required-entry input-text"  type="text" onblur="checkInt(this)"  value="<?php echo $gift->getDiscountStep(); ?>">
                        <?php else: ?>
                            <input id="discount_step" name="discount_step"  title="折扣步骤" class="required-entry input-text" onblur="checkInt(this)"  type="text"> 
                        <?php endif; ?>
                    </td>
                    <td class="label"><label for="name">赠<span class="required">*</span></label></td>
                    <td class="value">
                        <?php if($gift && $gift->getId()): ?>
                        <input id="discount_qty" name="discount_qty"  title="折扣数量" class="required-entry input-text" onblur="checkInt(this)"  type="text" value="<?php echo $gift->getDiscountQty(); ?>">
                        <?php else: ?>
                        <input id="discount_qty" name="discount_qty"  title="折扣数量" class="required-entry input-text"  onblur="checkInt(this)"  type="text">
                        <?php endif; ?>
                    </td>     
                </tr>
                <tr>
                    <td class="label"><label for="name">赠品总数<span class="required">*</span></label></td>
                    <td class="value">
                    <?php if($gift && $gift->getId()): ?>
                         <input id="gift_qty" name="gift_qty" onblur="checkInt(this)"  title="赠品总数" class="required-entry input-text"  type="text" value="<?php echo $gift->getGiftQty(); ?>">
                    <?php else: ?>
                        <input id="gift_qty" name="gift_qty" onblur="checkInt(this)"  title="赠品总数" class="required-entry input-text"  type="text">
                    <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td class="label"><label for="name">赠品限制个数<span class="required">*</span></label></td>
                    <td class="value">
                        <?php if($gift && $gift->getId()): ?>
                            <input id="customer_limit" name="customer_limit"  title="开始时间" class="required-entry input-text"  type="text" onblur="checkInt(this)" value="<?php echo $gift->getCustomerLimit(); ?>">
                        <?php else: ?>
                            <input id="customer_limit" name="customer_limit"  title="开始时间" class="required-entry input-text" onblur="checkInt(this)"  type="text">
                        <?php endif; ?>
                    </td>
                    <td class="label"><label for="name">订单取消，视为用户放弃活动参与权。客户参与次数不会退回。<span class="required">*</span></label></td>
                </tr>
                <tr>
                    <td class="label"><label for="name">访问地址<span class="required">*</span></label></td>
                    <td class="value">
                    <?php if($gift && $gift->getId()): ?>
                        <input id="url" name="url"  title="URL" class="required-entry input-text"  type="text" value="<?php echo $gift->getUrl(); ?>">
                    <?php else: ?>
                        <input id="url" name="url"  title="URL" class="required-entry input-text"  type="text">
                    <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td class="label"><label for="name">开始时间<span class="required">*</span></label></td>
                    <td class="value">
                    <?php if($gift && $gift->getId()): ?>
                        <input id="from_date" name="from_date" title="开始时间" class="input-text required-entry" type="text" value="<?php echo $gift->getFromDate(); ?>">
                        <img id="from_date_trig" class="v-middle" style="" title="Select Date" alt="" src="<?php echo $this->getSkinUrl('images/grid-cal.gif')?>">
                    <?php else: ?>
                        <input id="from_date" name="from_date"  title="开始时间" class="required-entry input-text" value="">
                        <img id="from_date_trig" class="v-middle" style="" title="Select Date" alt="" src="<?php echo $this->getSkinUrl('images/grid-cal.gif')?>">
                    <?php endif; ?>
                    </td>
                    <td class="label"><label for="name">结束时间<span class="required">*</span></label></td>
                    <td class="value">
                    <?php if($gift && $gift->getId()): ?>
                        <input id="to_date" name="to_date"  title="结束时间" class="required-entry input-text"  type="text" value="<?php echo $gift->getToDate(); ?>">
                        <img id="to_date_trig" class="v-middle" style="" title="Select Date" alt="" src="<?php echo $this->getSkinUrl('images/grid-cal.gif')?>">
                    <?php else: ?>
                        <input id="to_date" name="to_date"  title="结束时间" class="required-entry input-text" >
                        <img id="to_date_trig" class="v-middle" style="" title="Select Date" alt="" src="<?php echo $this->getSkinUrl('images/grid-cal.gif')?>">
                    <?php endif; ?>
                    </td>
                </tr>
                <tr>
                <tr>
                    <td class="label"><label for="comment">提示信息</label><span class="required">*</span></td>
                    <td class="value">
                    <?php if($gift && $gift->getId()): ?>
                        <textarea name="tip" id="tip" class="required-entry" cols="15" rows="1" style="height:100px"><?php echo $gift->getTip(); ?></textarea>  
                    <?php else: ?>   
                        <textarea name="tip" id="tip" class="required-entry" cols="15" rows="1" style="height:100px"></textarea>  
                    <?php endif; ?>
                    </td>
                    <td class="label"><label for="comment">简介</label><span class="required">*</span></td>
                    <td class="value">
                    <?php if($gift && $gift->getId()): ?>
                        <textarea name="description" id="description" class="required-entry" cols="15" rows="2" style="height:100px"><?php echo $gift->getDescription(); ?></textarea>
                    <?php else: ?> 
                        <textarea name="description" id="description" class="required-entry" cols="15" rows="2" style="height:100px"></textarea>
                    <?php endif; ?>
                    </td>
                </tr>
            </tbody>
        </table>
        </div>
    </div>
</form>

<div id="productlist" style="display:none">
    <div id="mask" style="display:none;"></div>
    <div class="entry-edit-head">
            <h4 class="icon-head head-edit-form fieldset-legend">搜索商品</h4>
    </div>
    <div class="fieldset  ignore-validate" id="coupons_information_fieldset">
        <div class="hor-scroll">
            <table cellspacing="0" class="form-list">
                <tbody>
                    <tr>
                        <td class="label"><label for="sku">商品名称</label></td>
                        <td class="value">
                            <input id="searchproductname" title="Dash Every X Characters" class="input-text" type="text"><p class="note"><span>请输入商品名称</span></p>
                        </td>
                    </tr>
                    <tr>
                        <td class="label"><label for="sku">SKU</label></td>
                        <td class="value">
                            <input id="sku" title="Dash Every X Characters" class="input-text" type="text"><p class="note" ><span>请输入sku</span></p>
                        </td>
                    </tr>
                    <tr>
                        <td class="label"><label for="sku">商品编号</label></td>
                        <td class="value">
                            <input id="searchproductid" title="Dash Every X Characters" class="input-text" type="text"><p class="note"><span>请输入商品编号</span></p>
                        </td>
                    </tr>
                    
                    <tr>
                        <td class="label"></td>
                        <td class="value">
                            <span id="coupons_generate_button"><button id="id_3cf81872242ced1ce5317b5704e032c7" title="搜索" type="button" class="scalable generate" onclick="check()"><span><span><span>搜索</span></span></span></button></span></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend">商品列表</h4>
        <div class="form-buttons"></div>
    </div>
    <div class="fieldset  ignore-validate" id="coupons_information_fieldset">
        <div class="grid np">
            <div class="hor-scroll">
            <table cellspacing="0" class="data order-tables">
            <colgroup>
            <col width="1">
            <col width="1">
            <col width="1">
            <col width="1">
            <col>
            </colgroup>
            <thead>
            <tr class="headings">
            <th style="width:50px;border-bottom:1px solid #CBD3D4;">商品编号</th>
            <th style="width:50px;border-bottom:1px solid #CBD3D4;"><span class="nobr">商品SKU</span></th>
            <th style="width:50px;border-bottom:1px solid #CBD3D4;"><span class="nobr">商品名称</span></th>
            <th style="width:50px;border-bottom:1px solid #CBD3D4;"><span class="nobr">商品图片</span></th>
            <th style="width:20px;border-bottom:1px solid #CBD3D4;"><span class="nobr">操作</span></th>
            </tr>
            </thead>
            <tbody class="even" id="appendproduct">

            </tbody>
            </table>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    //<![CDATA[
    Calendar.setup({
        inputField: "from_date",
        ifFormat: "%Y-%m-%e %H:%M:%S",
        showsTime: true,
        button: "from_date_trig",
        align: "Bl",
        singleClick : true
    });
    //]]>
</script>
<script type="text/javascript">
    //<![CDATA[
    Calendar.setup({
        inputField: "to_date",
        ifFormat: "%Y-%m-%e %H:%M:%S",
        showsTime: true,
        button: "to_date_trig",
        align: "Bl",
        singleClick : true
    });
    //]]>
</script>
<script type="text/javascript">
    var  $j = jQuery.noConflict();  

    $j(document).ready(function(){ 
        $j(".layui-layer-close").bind("click",function(){
              $j("#appendproduct").empty();
        });

    });

    function checkInt(k){
        if(isNaN(k.value)){
            layer.alert("请填写数字信息");
            k.value=null;
        }
    }



    function save(){
        var value = $j(".required-entry");
        var error =new Array();
        value.each(function(index,val){
            if(!$j(val).val()){
                error[index]=index;
            }
        });
        if(error.length){
            layer.alert("带*号都是必填项，请填写");
            return false;
        }
        var giftQty = $j("#gift_qty").val();
        var oldqty = $j("#oldqty").val();
        var wrong = giftQty-oldqty;
        if(wrong>0){
            layer.alert("赠品总数大于库存数");
            return false;
        }
        var stock = $j("#stock").val();
        if(stock==0){
            layer.alert("该赠品库存为缺货");
            return false;
        }
        $j("#submit_form").submit();
    }

    function add(i){
        var value=$j("#website").children('option:selected').val();
        if(value==0){
            layer.alert("请先选择站点");
            return false;
        }
        $j("#mask").html(i);
        var opendiv = layer.open({
             type: 1,
             title:"搜索管理",
             closeBtn:1,
             scrollbar:true,
             area: ['800px', '730px'],
             fix: false,
             maxmin: false,
             content: $j('#productlist'),  
             cancel:function(){
                 $j("#appendproduct").empty();
             }
        });
    }

    function check(){
        var website=$j("#website").children('option:selected').val();
        var sku = $j("#sku").val();
        var productId = $j("#searchproductid").val();
        var productname = $j("#searchproductname").val();

        if(!sku && !productId && !productname){
            layer.alert("请输入搜索内容");
            return false;
        }

        var mask = $j("#mask").html();
        var url ="<?php echo $this->getUrl('admin_promotion/adminhtml_gift/productlist'); ?>"+"mask/"+mask;
        if(sku){
            var url = url + "/sku/"+sku+"/websiteId/"+website;
        }else if(productId){
            var url = url + "/product_id/"+productId+"/websiteId/"+website;
        }else if(productname){
            var url = url +"/websiteId/"+website+ "/name/"+productname;
        }

        $j.get(url,function(result){
            if(result.error){
                layer.alert(result.message);
            }else{
                var html = result.message;
                $j("#appendproduct").empty();
                $j("#appendproduct").append(html); 
               $j(".addtolist").bind("click",function(){
                    var addtolist = $j(this);
                    var productId = addtolist.parent().parent(".parent").children(".productId").html();
                    var productname = addtolist.parent().parent(".parent").children(".productname").html();
                    var sku = addtolist.parent().parent(".parent").children(".productsku").html();
                    var image = addtolist.parent().parent(".parent").children(".image").html();
                    var product_name_show =  $j("#product_name_show");
                    product_name_show.css("display","block");
                    $j("#product_name").val(productname);
                    $j("#productid").val(productId);
                    $j(".usku").html(sku);
                    $j(".uimage").html(image);
                    $j(".uproductname").html(productname);

                    $j("#appendproduct").empty();
                    layer.closeAll();
                    layer.msg("商品已加入到表单中");
                    $j("#addproducts").html("修改商品");
               });
               $j(".addtolistgift").bind("click",function(){
                    var addtolist = $j(this);
                    var oldqty = addtolist.parent().parent(".parent").children(".productId").attr("qty");
                    var stock = addtolist.parent().parent(".parent").children(".productId").attr("stock");
                    var productId = addtolist.parent().parent(".parent").children(".productId").html();
                    var productname = addtolist.parent().parent(".parent").children(".productname").html();
                    var sku = addtolist.parent().parent(".parent").children(".productsku").html();
                    var image = addtolist.parent().parent(".parent").children(".image").html();
                    var product_name_show =  $j("#gift_product_show");
                    product_name_show.css("display","block");
                    $j("#gift_product_name").val(productname);
                    $j("#giftproductid").val(productId);
                    $j("#oldqty").val(oldqty);
                    $j("#stock").val(stock);
                    $j(".gsku").html(sku);
                    $j(".gimage").html(image);
                    $j(".gproductname").html(productname);
   
                    $j("#appendproduct").empty();
                    layer.closeAll();
                    layer.msg("赠品已加入到表单中");
                    $j("#addgiftproducts").html("修改赠品");
               });
            }

         },"json");
    }

</script>